/*
 * GamelistPanel.java
 *
 * Created on September 28, 2008, 1:09 PM
 */
package no.eirikb.bomberman.applet.menu;

import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import no.eirikb.bomberman.applet.Bomberman;
import no.eirikb.bomberman.shared.Game;
import no.eirikb.bomberman.shared.User;

/**
 *
 * @author  eirikb
 */
public class GamelistPanel extends javax.swing.JPanel {

    private Bomberman bomberman;

    /** Creates new form GamelistPanel */
    public GamelistPanel(Bomberman bomberman) {
        this.bomberman = bomberman;
        initComponents();
    }

    public void updateGameList(Game[] games) {
        DefaultListModel model = (DefaultListModel) gameList.getModel();
        model.clear();
        for (Game game : games) {
            model.addElement(game);
        }
    }

    public void addGame(Game game) {
        DefaultListModel model = (DefaultListModel) gameList.getModel();
        for (int i = 0; i < model.size(); i++) {
            Game game2 = (Game) model.get(i);
            if (game2.getName().equals(game.getName())) {
                model.remove(i);
                break;
            }
        }
        model.addElement(game);
    }

    public void removeGame(Game game) {
        DefaultListModel model = (DefaultListModel) gameList.getModel();
        for (int i = 0; i < model.size(); i++) {
            Game game2 = (Game) model.get(i);
            if (game2.getName().equals(game.getName())) {
                model.remove(i);
                return;
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headerLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        gameList = new javax.swing.JList();
        joinButton = new javax.swing.JButton();

        setBackground(java.awt.Color.yellow);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        headerLabel.setFont(new java.awt.Font("Dialog", 1, 24));
        headerLabel.setText("Gamelist"); // NOI18N

        gameList.setModel(new DefaultListModel());
        gameList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        gameList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                gameListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(gameList);

        joinButton.setText("JOIN");
        joinButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                joinButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(joinButton))
                    .addComponent(headerLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headerLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(joinButton)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void joinButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_joinButtonActionPerformed
    if (gameList.getSelectedIndex() >= 0) {
        try {
            Game game = (Game) gameList.getSelectedValue();
            User user = bomberman.getUser();
            if (bomberman.getServer().joinGame(bomberman.getHash(), user.getNick(), game.getName())) {
                user.setGameName(game.getName());
                game.addUser(user);
                bomberman.showGameWaitPanel(game);
            } else {
                user.setGameName(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}//GEN-LAST:event_joinButtonActionPerformed

private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
    System.out.println("LOL");
    try {
        updateGameList(bomberman.getServer().getGameList());//GEN-LAST:event_formComponentShown
        } catch (RemoteException ex) {
            Logger.getLogger(GamelistPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

private void gameListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_gameListMouseClicked
    if (evt.getClickCount() == 2) {
        joinButton.doClick();
    }
}//GEN-LAST:event_gameListMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList gameList;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton joinButton;
    // End of variables declaration//GEN-END:variables
}
